--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local config = require "configquery"
require "common"

local AssertEx = AssertEx

local Command = class("Command")
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.msg = message 
	self.machine = machine 
	self.is_open = true
	self.close  = false
	self.tableid = self.machine:getTableId()
	self:register()
end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('safe_close',handler(self,self.safeClose))
	self.msg:registerSelf('update_prop',handler(self,self.updateProperty))
	self.msg:registerSelf('broadcast',handler(self,self.broadcast))
	self.msg:registerSelf('update_game_pot',handler(self,self.updateGamePot))
	
	self.msg:registerSelf('gm_set_code_req',handler(self,self.gmSet)) --配牌

	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))	
	self.msg:registerSelf('lost_connect',handler(self,self.lostConnect))
	-- self.msg:registerSelf('reconnect_req',handler(self,self.reconnect)) 

	self.msg:registerSelf('zjh_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('zjh_oper_req',handler(self,self.oper))
	self.msg:registerSelf('zjh_look_req',handler(self,self.look))
	self.msg:registerSelf('zjh_compare_req',handler(self,self.compare))
	self.msg:registerSelf('zjh_com_seat_req',handler(self,self.compareSeat))
	self.msg:registerSelf('zjh_giveup_req',handler(self,self.giveup))	
end


---------------------------------------------------------
-- CMD
---------------------------------------------------------

--更新属性
function Command:updateProperty(playerid, data)
	print("_________________大厅发来玩家数据___",data)
	self.machine:updateProperty(playerid,data)
	return true
end

--广播
function Command:broadcast( pack )
	return true
end

--必须要这个接口才能同步后台更新库存
function Command:updateGamePot(pot_info)
	--更新库存
	return true
end

--安全关服
function Command:safeClose(close)
	self.close = close
	print("_____1111111111________setClose___",close)
	self.machine:setClose(close)
end

---------------------------------------------------------
-- REQUEST
---------------------------------------------------------
--进入桌子
function Command:enterTable(playerid,player_info)
	print("_______enterTable________self.close__",self.close)
	AssertEx(not self.close, 'enter_table_res' , 7)
	AssertEx(playerid > 0 , 'enter_table_res' , 8)
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			print("_____没有断线重复请求进入桌子________")
		end
	else
		--第一次进入
		player = table.copy(player_info)
		self.machine:addPlayer(player)
		--符合条件，自动坐下
		local bSeat = self.machine:sitDown(playerid)
		--座位已满
		AssertEx(bSeat , 'enter_table_res' , 18)
	end
	player.disconnect = false
	local result = {}
	result.enter_table_res = {
		tableid = self.tableid,
		gameid = getGameIdbyTable(self.tableid),
	}
	return result
end

--离开
function Command:leaveTable(playerid)
	--print("___________zjh_leave_table_req_________1_____")
	local player = self.machine:getPlayer(playerid)
	if player then									
		if not player.playing  or self.machine:getState() == 'rest' then 
			self.machine:leaveTable(playerid)
			return {leave_table_res = {tableid = self.tableid}}
		elseif player.playing and (player.giveup or player.lose) then 
			self.machine:leaveTable(playerid)
			return {leave_table_res = {tableid = self.tableid}}			
		end		
		--游戏已开始，请在休息时再退出！
		return {leave_table_res = GetErrorTable(9)}
	else
		--未进入房间
		return {leave_table_res = GetErrorTable(10)}		
	end
	return false
end

--掉线
function Command:lostConnect(playerid)
	local kickPlayer = false
	local player = self.machine:getPlayer(playerid)
	if player then
		player.disconnect = true		
		kickPlayer = self.machine:disconnect(playerid)
	else
		--log.error('>>>>>>>>>>>>>>>>>>>>############lost_connect no found playerid:',playerid)
		print('>>>>>>>>>>>>>>>>>>>>############玩家掉线，但游戏中不存在此玩家:',playerid)
	end
	return kickPlayer
end

-- 得到桌子状态
function Command:getStatus( playerid )
	local player = self.machine:getPlayer(playerid)
	--未进入桌子
	AssertEx(player , 'zjh_status_res', 10)
	--重连进入
	player.disconnect = false
	player.ready = true --在没有ready前不发消息给此玩家
	local status = self.machine:getTableStatus(playerid)
	
	return {zjh_status_res = status}
end

--GM命令
function Command:gmSet( playerid,cards ,repeatNum)
	self.machine:setGmCard(playerid,cards,repeatNum)
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------

--操作
function Command:oper( playerid,  args)
	local player = self.machine:getPlayer(playerid)
	local operSeatid = self.machine:getOperSeatid()
	local state = self.machine:getState()
	if state ~= 'oper' then 
		return 
	end	
	--无效操作
	AssertEx(player and self.machine:isPlaying(player) and operSeatid==player.seatid, 'zjh_oper_res', 16)
	local b = self.machine:playerAddChip(playerid,args)
	--无效操作
	AssertEx(b, 'zjh_oper_res',16)
	--推送玩家状态
	return {zjh_oper_res={success=b}}	
end

--看牌
function Command:look(playerid,args)
	local player = self.machine:getPlayer(playerid)
	--无效操作
	AssertEx(player and player.playing, 'zjh_look_res',16)
	local state = self.machine:getState()
	if state ~= 'oper' then 
		return 
	end
	local data = self.machine:playerLook(player)
	--无效操作
	AssertEx(data, 'zjh_look_res',16)
	--推送玩家状态
	return {zjh_look_res=data}
end

--比牌
function Command:compare(playerid,args)
	local player = self.machine:getPlayer(playerid)	
	local operSeatid = self.machine:getOperSeatid()
	local state = self.machine:getState()
	--无效操作
	AssertEx(player and player.playing and operSeatid==player.seatid, 'zjh_compare_res', 16)
	if state ~= 'oper' then 
		return 
	end
	local b,data = self.machine:playerCompare(playerid)
	--无效操作
	AssertEx(b, 'zjh_compare_res',16)
	-- print("___________copare_res__",data)
	return data
end

--比牌确定座位
function Command:compareSeat(playerid,args)
	local player = self.machine:getPlayer(playerid)	
	local operSeatid = self.machine:getOperSeatid()
	local state = self.machine:getState()
	--无效操作
	AssertEx(player and player.playing and operSeatid==player.seatid, 'zjh_com_seat_res', 16)	
	if state ~= 'compare' then 
		return
	end
	local seatid = args.seatid
	if seatid < 1 or seatid > self.machine.maxSeatNum then 
		return 
	end
	local b = self.machine:setPlayerCompare(playerid,seatid,true)
	--推送玩家状态
	return {zjh_com_seat_res={win=b}}	
end

--弃牌
function Command:giveup(playerid,args)
	local player = self.machine:getPlayer(playerid)		
	--无效操作
	AssertEx(player and player.playing , 'zjh_giveup_res', 16)	
	local state = self.machine:getState()
	if state ~= 'oper' then 
		return 
	end

	local b = self.machine:giveup(player.seatid)
	--推送玩家状态
	return {zjh_giveup_res={success=b}}		
end

function Command:test( ... )
	print("____________test_______________111122")
	--self.machine:setClose(true)
end


return Command